/*
 * Created by JFormDesigner on Mon Jun 21 10:53:07 GMT+08:00 2021
 */

package com.bk.view;

import java.awt.event.*;

import com.bk.dao.BookTypeDao;
import com.bk.model.BookType;
import com.bk.util.ToolUtil;

import java.awt.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 图书类型编辑窗体
public class btEditFrame extends JFrame {
    BookType booktype;

    public btEditFrame(BookType booktype) {
        initComponents();
        this.booktype = booktype;
        textField1.setText(booktype.getBtname());
    }

    // 编辑，监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String btname = textField1.getText();
        if (new ToolUtil().judgeIdOrName(btname.trim())) {
            JOptionPane.showMessageDialog(null, "类型名不可以是纯数字！");
            return;
        }
        try {
            if (new BookTypeDao().editBookType(booktype, btname)){
                JOptionPane.showMessageDialog(null, "编辑成功");
            }else{
                JOptionPane.showMessageDialog(null, "编辑失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        dispose();
    }

    private void textField1KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField1.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u56fe\u4e66\u7c7b\u578b\u7f16\u8f91");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u7c7b\u578b\u540d");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- textField1 ----
        textField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField1KeyTyped(e);
            }
        });

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u7f16\u8f91");
        button1.addActionListener(e -> button1ActionPerformed(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(button1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1)
                            .addGap(18, 18, 18)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(31, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(26, 26, 26)
                    .addComponent(button1)
                    .addContainerGap(30, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
